// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // item:0,
    tab: 0,
    playlist: [{
      id: 1,
      title: '夜空中最亮的星',
      singer: '逃跑计划',
      src: 'http://172.16.26.249:3000/1.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 2,
      title: '奏鸣曲',
      singer: '莫扎特',
      src: 'http://172.16.26.249:3000/2.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 3,
      title: '欢乐颂',
      singer: '贝多芬',
      src: 'http://172.16.26.249:3000/3.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 4,
      title: '爱之梦',
      singer: '李斯特',
      src: 'http://172.16.26.249:3000/4.mp3',
      coverImgUrl: '/images/cover.jpg'
    }],
    state: 'paused',
    playIndex: 0,
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '未播放',
      singer: '未知',
      coverImgUrl: '/images/cover.jpg'
    }
  },

  changeItem: function (e) {
    //console.log(e.target.dataset.item);
    this.setData({
      tab: e.target.dataset.item
    });
  },

  changeTab: function (e) {
    this.setData({
      tab: e.detail.current
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
 
  audioCtx: null,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createInnerAudioContext();
    //this.audioCtx.src = 'http://172.16.26.249:3000/1.mp3';
    //this.audioCtx.play();
    this.setMusic(0);

    //错误
    this.audioCtx.onError(() => {
      console.log('播放失败:' + this.audioCtx.src);
    })
    //播放结束
    this.audioCtx.onEnded(() => {
      this.next()
    })

    this.audioCtx.onPlay(function () {})

    this.audioCtx.onTimeUpdate(() => {
      this.setData({
        'play.currentTime': formatTime(this.audioCtx.currentTime),
        'play.duration': formatTime(this.audioCtx.duration),
        'play.percent': this.audioCtx.currentTime / this.audioCtx.duration * 100
      })
    })

    function formatTime(time) {
      let minute = Math.floor(time / 60) % 60
      let second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    }
  },
  next() {
    let index = this.data.playIndex
    this.setMusic(index)
    if (this.data.state === 'running') {
      this.play()
    }
  },
  setMusic(index) {
    var music = this.data.playlist[index];
    this.audioCtx.src = music.src;
    this.setData({
      playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImgUrl,
      'play.currentTime': '00:00',
      'play.duration': '00:00',
      'play.percent': 0
    });
    // this.audioCtx.play();
  },
  sliderChange(e) {
    let second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.pause()
    this.audioCtx.seek(second)
    setTimeout(() => {
      this.audioCtx.play()
    }, 500)
  },
  play() {
    this.audioCtx.play();
    this.setData({
      state: 'running'
    });
  },

  pause() {
    this.audioCtx.pause();
    this.setData({
      state: 'paused'
    });
  },

  next() {
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1;
    console.log(index);
    this.setMusic(index);
    if (this.data.state === 'running') {
      this.play();
    }
  },
  btns(e) {
    console.log(e);
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})